package com.echo.register.user.api;

import com.echo.register.common.result.Result;
import com.echo.register.common.util.AuthContextHolder;
import com.echo.register.common.util.IpUtil;
import com.echo.register.model.user.UserInfo;
import com.echo.register.user.service.UserInfoService;
import com.echo.register.vo.user.LoginVo;
import com.echo.register.vo.user.UserAuthVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName: UserInfoApiController
 * @Package: com.echo.register.user.api
 * @Description:
 * @Author 谢同学
 * @Create 2023/12/22 19:09
 * @Version 1.0
 */
@RestController
@Api(tags = "用户管理")
@RequestMapping("/api/user")
public class UserInfoApiController {
    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public Result<Map<String,Object>> login(@RequestBody LoginVo loginVo, HttpServletRequest request){
        // 设置ip
        loginVo.setIp(IpUtil.getIpAddr(request));
        return Result.ok(userInfoService.login(loginVo));
    }
    @ApiOperation("用户认证")
    @PostMapping("auth/userAuth")
    public Result<?> userAuth(@RequestBody UserAuthVo userAuthVo,
                              HttpServletRequest request){
        userInfoService.userAuth(AuthContextHolder.getUserId(request),userAuthVo);
        return Result.ok();
    }

    @ApiOperation("获取用户id信息接口")  // 用于在前端回显
    @GetMapping("auth/getUserInfo")
    public Result<UserInfo> getUserInfo(HttpServletRequest request) {
        UserInfo userInfo = userInfoService.getById(AuthContextHolder.getUserId(request));
        return Result.ok(userInfo);
    }

}
